#!/bin/sh
export logtag="udisk"

source /etc/colorc

#OTA状态文件默认目录
export otamgr_dir=/vend

#OTA状态文件名称
export obmota_sta_file=".obmota_sta_file"

#输出函数
export msgdbg=0
msglog()
{
	export logroot=/dev
	export logfile=console
	if [ ! -d ${logroot} ]; then
		mkdir -p ${logroot}
	fi
	tmpstr=$*
	if [ -n "$(/bin/echo   ${tmpstr} | /bin/grep -w OK)" ]; then
		sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w START)" ]; then
		swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w ERROR)" ]; then
		serro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w FAILED)" ]; then
		perro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w DONE)" ]; then
		pdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w FINISH)" ]; then
		sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w TMO)" ]; then
		swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
	else
		if [ $msgdbg -ne 0 ]; then
			if [ -n "$(/bin/echo   ${tmpstr} | /bin/grep -w ok)" ]; then
				sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w start)" ]; then
				swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w error)" ]; then
				serro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w failed)" ]; then
				perro "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w done)" ]; then
				pdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w finish)" ]; then
				sdone "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			elif [ -n "$(/bin/echo ${tmpstr} | /bin/grep -w tmo)" ]; then
				swarn "${logtag}: ${tmpstr}" > ${logroot}/${logfile}
			else
				sinfo "${logtag}: $*" > ${logroot}/${logfile}
			fi
		fi
	fi
}

wait_ual_mount()
{
	is_ual_mnt=$(/bin/mount|/bin/grep tmpfs|/bin/grep ual)
	if [ -n "$is_ual_mnt" ]; then
		return 0
	fi

	CNT=10
	while true; do
		is_ual_mnt=$(/bin/mount|/bin/grep tmpfs|/bin/grep ual)
		if [ -n "$is_ual_mnt" ]; then
			return 0
		fi
		CNT=`expr $CNT - 1`
		if [ $CNT -eq 0 ]; then
			return 1
		fi
		/bin/usleep 100000
	done
	return 1
}

check_vend_ismount()
{
	is_vend_mnt=$(/bin/mount|/bin/grep /vend)
	if [ -z "${is_vend_mnt}" ]; then
		msglog "vend not mountd!"
		return 1
	fi
	return 0
}

check_root_ismount()
{
	export is_root_mnt=$(/bin/mount|/bin/grep /root)
	if [ -z "${is_root_mnt}" ]; then
		msglog "root is not mount!"
		return 1
	fi
	return 0
}

check_udisk_ismount()
{
	export is_udisk_mnt=$(/bin/mount|/bin/grep udisk)
	if [ -z "${is_udisk_mnt}" ]; then
		return 1
	fi
	msglog "udisk already mounted!"
	return 0
}

#改变OTA状态
change_obmota_sta()
{
	tmpsta=$1
	export OTA_STA_NOW=${tmpsta}
	/bin/echo "$1" > ${otamgr_dir}/${obmota_sta_file}
	/bin/sync
	msglog "obmota change state to ${tmpsta}"
}

#检查MD5码是否一致
check_otaimg_md5()
{
	pack_md5=$(/usr/bin/md5sum ${1}/${2} |/usr/bin/awk {'print $1'})
	file_md5=$(/bin/cat ${1}/${3})
	if [ "x${pack_md5}" = "x${file_md5}" ]; then
		return 0
	fi
	return 1
}

#检查升级文件是否最新
check_otaimg_isnew()
{
	save_fpath=/vend/${2}.old
	if [ ! -f ${save_fpath} ]; then
		return 0
	fi
	save_md5=$(/bin/cat ${save_fpath})
	file_md5=$(/bin/cat ${1}/${2})
	if [ "x${save_md5}" != "x${file_md5}" ]; then
		return 0
	fi
	return 1
}

#是否允许检查OBMOTA固件文件
is_obmota_check_enable()
{
	#读取OBMOTA状态文件/vend/.otasta
	if [ ! -f /${otamgr_dir}/${obmota_sta_file} ]; then
		msglog "obmota create new state file: ${otamgr_dir}/${obmota_sta_file}"
		#切换到idle状态
		change_obmota_sta "idle"
	else
		export OTA_STA_NOW=$(/bin/cat ${otamgr_dir}/${obmota_sta_file})
		msglog "obmota find state file ${otamgr_dir}/${obmota_sta_file}"
	fi
	msglog "obmota get current state ${OTA_STA_NOW}"
	check_root_ismount
	if [ $? -ne 0 ]; then
		#不允许检查OBMOTA固件文件
		msglog "obmota check disabled because root not mount" 
		return 1
	fi
	#允许检查OBMOTA固件文件
	msglog "obmota check enable" 
	return 0
}

#检测OBMOTA固件文件是否有效
check_otaimg_isvalid()
{
	validcnt=0
	otaimg_dir=$1
	msglog "check otaimg files in ${otaimg_dir} [start]"

	msglog "check data otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_data.tar.gz ]&&[ -f ${otaimg_dir}/update_data.md5 ]; then
		#动态开启调试信息
		export msgdbg=1
		msglog "check data otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check data otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_data.tar.gz update_data.md5
		if [ $? -eq 0 ]; then
			msglog "check data otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check data otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_data.md5
			if [ $? -eq 0 ]; then
				msglog "check data otaimg isnew [OK]"
				let validcnt++
			else
				msglog "check data otaimg isnew [ERROR]"
			fi
		else
			msglog "check data otaimg md5sum [ERROR]"
		fi
	else
		msglog "check data otaimg exist [error]"
	fi

	msglog "check root otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_root.tar.gz ]&&[ -f ${otaimg_dir}/update_root.md5 ]; then
		#动态开启调试信息
		export msgdbg=1
		msglog "check root otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check root otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_root.tar.gz update_root.md5
		if [ $? -eq 0 ]; then
			msglog "check root otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check root otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_root.md5
			if [ $? -eq 0 ]; then
				msglog "check root otaimg isnew [OK]"
				let validcnt++
			else
				msglog "check root otaimg isnew [ERROR]"
			fi
		else
			msglog "check root otaimg md5sum [ERROR]"
		fi
	else
		msglog "check root otaimg exist [error]"
	fi

	msglog "check rtfs otaimg exist [start]"
	if [ -f ${otaimg_dir}/update_rtfs.tar.gz ]&&[ -f ${otaimg_dir}/update_rtfs.md5 ]; then
		#动态开启调试信息
		export msgdbg=1
		msglog "check rtfs otaimg exist [OK]"
		#校验固件文件MD5
		msglog "check rtfs otaimg md5sum [START]"
		check_otaimg_md5 ${otaimg_dir} update_rtfs.tar.gz update_rtfs.md5
		if [ $? -eq 0 ]; then
			msglog "check rtfs otaimg md5sum [OK]"
			#校验固件是否是最新
			msglog "check rtfs otaimg isnew [START]"
			check_otaimg_isnew ${otaimg_dir} update_rtfs.md5
			if [ $? -eq 0 ]; then
				msglog "check rtfs otaimg isnew [OK]"
				let validcnt++
			else
				msglog "check rtfs otaimg isnew [ERROR]"
			fi
		else
			msglog "check rtfs otaimg md5sum [ERROR]"
		fi
	else
		msglog "check rtfs otaimg exist [error]"
	fi

	if [ ${validcnt} -ne 0 ]; then
		#表示检测成功
		msglog "check otaimg files in ${otaimg_dir} [OK]"
		return 0
	fi
	#表示检测失败
	msglog "check otaimg files in ${otaimg_dir} [error]"
	return 1
}

main_proc()
{
	export mntdev_name=$1

	#不支持/ual以外的其他挂载逻辑
	if [ ! -d /ual ]; then
		msglog "check ual frame [ERROR]"
	    return 1
	fi

	#等待UAL被挂载
	wait_ual_mount
	if [ $? -ne 0 ]; then
		msglog "wait ual ready [ERROR]"
		return 1
	fi

	if [ "$mntdev_name" = "" ]; then
	   msglog "check mount device [ERROR]"
	   return 1
	elif [ "$mntdev_name" = "netotareboot" ]; then
		#网络OTA升级
		#------------------------------------------------------------------
		#需要保证/vend已经被挂载
		check_vend_ismount
		if [ $? -ne 0 ]; then
			msglog "netotareboot check vend mount [ERROR]"
			return 1
		fi

		#是否允许检查OBMOTA固件文件
		is_obmota_check_enable
		if [ $? -ne 0 ]; then
			#不允许执行OBMOTA升级固件文件检查
			msglog "netotareboot check obmsta [ERROR]"
			return 1
		fi

		#检测OTA固件文件是否存在并有效?
		check_otaimg_isvalid /vend
		if [ $? -eq 0 ]; then
			msglog "netotareboot check otaimg valid [DONE]"
			#切换到网络升级状态
			change_obmota_sta "netotareboot"
			msglog "netotareboot query system reboot [START]"
			/bin/sync
			/sbin/reboot
		else
			msglog "netotareboot check otaimg valid [FAILED]"
		fi
		#------------------------------------------------------------------
	else
		#U盘挂载/卸载/OTA升级
		#------------------------------------------------------------------

		#强制设定U盘挂载目录
		export mntdir="/ual/udisk"

		if [ "$ACTION" = "remove" ]; then
			#卸载
			#------------------------------------------------------------------
			/bin/umount -lf "${mntdir}" 
			/bin/rm     -rf "${mntdir}"
			msglog "umount ${mntdir} [DONE]"
			#------------------------------------------------------------------
		else
			#挂载
			#------------------------------------------------------------------
			if [ ! -d "${mntdir}" ]; then
				/bin/mkdir -p "${mntdir}"
			fi
			check_udisk_ismount
			if [ $? -ne 0 ]; then
				/bin/mount -t vfat /dev/$1 "${mntdir}" > /dev/null 2>&1
				if [ $? -ne 0 ]; then
					msglog "mount /dev/$1 ${mntdir} [FAILED]"
					/bin/rm -rf "${mntdir}" > /dev/null 2>&1
					return 1
				else
					msglog "mount /dev/$1 ${mntdir} [DONE]"
				fi
			fi
		   #------------------------------------------------------------------

		   #是否允许检查OBMOTA固件文件
		   is_obmota_check_enable
		   if [ $? -ne 0 ]; then
			   #不允许执行OBMOTA升级固件文件检查
				msglog "usbotareboot check obmsta [error]"
			   return 1
		   fi
		   
		   #检测OTA固件文件是否存在并有效?
		   check_otaimg_isvalid ${mntdir}
		   if [ $? -eq 0 ]; then
			   msglog "usbotareboot check otaimg valid [DONE]"
			   #切换到U盘升级状态
			   #change_obmota_sta "usbotareboot"
			   msglog "usbotareboot query system reboot [START]"
			   /bin/sync
			   /sbin/reboot
		   else
			   msglog "usbotareboot check otaimg valid [failed]"
		   fi
			#------------------------------------------------------------------
		fi #if [ "$ACTION" = "remove" ]; then
	fi #if [ "$mntdev_name" = "" ]; then
}
main_proc $@
